package ai.json;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

import org.json.JSONArray;
import org.json.JSONObject;

import ai.application.Config;
import ai.astar.Link;
import ai.astar.Node;

public class JsonImport {
	
	public static Hashtable<String, Node> importJsonFile(String fileName) {

		Hashtable<String, Node> nodesHashtable = new Hashtable<String, Node>();
		Vector<Link> linksVector = new Vector<Link>();
		double minX=Double.MAX_VALUE, maxX=0, minY=Double.MAX_VALUE, maxY=0;
		
		try {
			JSONObject req = new JSONObject(readFile(fileName));
			JSONObject  network     = req.getJSONObject("network");
			JSONArray nodes     = network.getJSONArray("nodes");
			JSONArray links     = network.getJSONArray("links");

			for(int i=0; i<nodes.length(); i++){
				JSONObject nodeWrapperObject = (JSONObject) nodes.get(i);
				JSONObject  nodeObject = (JSONObject) nodeWrapperObject.get("node");
		        String id = nodeObject.getString("id");
		        double x = Double.parseDouble(nodeObject.getString("x"));
		        double y = Double.parseDouble(nodeObject.getString("y"));
		        
		        minX = x < minX ? x : minX;
		        maxX = x > maxX ? x : maxX;
		        minY = y < minY ? y : minY;
		        maxY = y > maxY ? y : maxY;
		        
				nodesHashtable.put(id, new Node(id, x,y));
			}
			minX -= 2;
			minY -= 2;
			
			double sizeX = maxX-minX;
			double sizeY = maxY-minY;
			double scaleX = sizeX / Config.WINDOW_SIZE_X;
			double scaleY = sizeY / Config.WINDOW_SIZE_Y;
			double resultingScale = scaleX > scaleY ? scaleX : scaleY;
			
			Enumeration<String> keys = nodesHashtable.keys();
			while( keys.hasMoreElements() ) {
			  Object key = keys.nextElement();
			  Node node = (Node) nodesHashtable.get(key);
			  node.setX((node.getX()-minX)/resultingScale);
			  node.setY((sizeY-(node.getY()-minY))/resultingScale);
			}
			
			for(int i=0; i<links.length(); i++){
				JSONObject linkWrapperObject = (JSONObject) links.get(i);
				JSONObject  linkObject = (JSONObject) linkWrapperObject.get("link");
		        String from = linkObject.getString("from");
		        String to = linkObject.getString("to");
		        double length = Double.parseDouble(linkObject.getString("length")) / resultingScale;
		        Node fromNode = nodesHashtable.get(from);
		        Node toNode = nodesHashtable.get(to);
		        if(fromNode != null && toNode != null){
		        	Link link = new Link(fromNode, toNode, length);
		        	fromNode.getNeighbors().add(link);
		        	linksVector.add(link);
		        }
			}
			
		} catch (Exception exception) {
			exception.printStackTrace();
		}
		
		return nodesHashtable;
	}
	
	private static String readFile( String file ) throws IOException {
	    BufferedReader reader = new BufferedReader( new FileReader (file));
	    String line  = null;
	    StringBuilder stringBuilder = new StringBuilder();
	    String ls = System.getProperty("line.separator");
	    while( ( line = reader.readLine() ) != null ) {
	        stringBuilder.append( line );
	        stringBuilder.append( ls );
	    }
	    return stringBuilder.toString();
	 }
	
	public void importJsonFile(){
		
	}
}
